<?php
/**
 * @Author: king
 * @Last Modified by:   king
 * Date: 2018/7/11
 * Time: 16:04
 */
namespace App\Tools;

use OSS\OssClient;
use OSS\Core\OssException;

/**
 * 阿里云OSS文件上传
 * Class AliOss
 * @package app\common\lib
 */
class AliOss
{
    private static $instance;
    private $oss_config;
    private $put_object  = 'object'; //将文件写入oss
    private $upload_file = 'file';   //上传文件到oss

    /* 获取数据库oss配置,当然你可以自己写在该类中以变量定义
     * 构造方法为私有防止类被实例化
     */
    private function __construct()
    {
        $this->oss_config = [
            'access_key_id' => config('aliyun.oss_access_id'),
            'access_key_secret' => config('aliyun.oss_access_key'),
            'endpoint' => config('aliyun.oss_endpoint'),
            'bucket' => config('aliyun.oss_bucket'),
        ];
    }
    /**
     * 克隆方法为私有，防止被克隆
     */
    private function __clone()
    {
        // TODO: Implement __clone() method.
    }

    //单例模式
    public static function getInstance()
    {
        if(!self::$instance instanceof  AliOss)
        {
            self::$instance = new self();
        }
        return self::$instance;
    }

    /**
     * @return array|OssClient
     * 创建oss对象
     */
    public function clientOss()
    {
        if(empty($this->oss_config['access_key_id']) || !isset($this->oss_config['access_key_id']))
        {
            return ['errorMsg' => 'access key id invalid'];
        }
        if(empty($this->oss_config['access_key_secret']) || !isset($this->oss_config['access_key_secret']))
        {
            return ['errorMsg' => 'access key secret invalid'];
        }
        if(empty($this->oss_config['endpoint']) || !isset($this->oss_config['endpoint']))
        {
            return ['errorMsg' => 'endpoint invalid'];
        }
        try
        {
            $oss_client = new OssClient($this->oss_config['access_key_id'],$this->oss_config['access_key_secret'],$this->oss_config['endpoint']);
        }
        catch (OssException $e)
        {
            return ['errorMsg' => $e->getMessage()];
        }
        return $oss_client;
    }

    /**
     * @param $path string oss保存的文件路径（如果没有将会自动创建）
     * @param $file string  文件内容
     * @param $action string 上传类型
     * @return array|bool
     */
    function ossUpload($path,$file,$action = 'object')
    {
        $oss_client = $this->clientOss();

        switch ($action)
        {
            case $this->put_object === $action:
                try
                {
                   $a = $oss_client->putObject($this->oss_config['bucket'],$path,$file);
                }
                catch(OssException $e)
                {
                    return ['errorMsg' => $e->getMessage()];
                }
                return true;
            case $this->upload_file === $action:
                try
                {
                    $oss_client->uploadFile($this->oss_config['bucket'],$path,$file);
                }catch (OssException $e){
                    return ['errorMsg' => $e->getMessage()];
                }
                return true;
            default:
        }
        return true;
    }


}
